package com.eye.catcher.controller;


import com.eye.catcher.param.CommonResponse;
import com.eye.catcher.param.ResponseData;
import com.eye.catcher.param.ResponseUtil;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.entity.TSysDaUserRole;
import com.eye.catcher.rabbit.MQDto;
import com.eye.catcher.service.generate.ITSysDaUserRoleService;
import com.eye.catcher.system.userRole.param.UserRoleDto;
import com.eye.catcher.system.userRole.param.UserRolePage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huzhiwei
 * @since 2022-02-08
 */
@Slf4j
@Controller
@RequestMapping("/eyeCatcher-system/sys-user-role")
public class TSysDaUserRoleController extends TSysDaBaseController {
    private final static String MODULE_CODE = "/sys-user-role";
    private final static String MODULE_NAME = "用户角色关联权限";

    @Autowired
    private ITSysDaUserRoleService userRoleService;


    /**
     *
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRole
     * @return
     */
    @PostMapping("/addUserRole")
    @ResponseBody
    public ResponseData addUserRole(@RequestBody TSysDaUserRole userRole) {
        String methodCode = "/addUserRole";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,userRole.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.addUserRole(userRole);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     *
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRole
     * @return
     */
    @PostMapping("/addBatchUserRole")
    @ResponseBody
    public ResponseData addBatchUserRole(@RequestBody TSysDaUserRole userRole) {
        String methodCode = "/addBatchUserRole";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,userRole.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.addBatchUserRole(userRole);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    /**
     *
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRole
     * @return
     */
    @PostMapping("/addBatchRoleUser")
    @ResponseBody
    public ResponseData addBatchRoleUser(@RequestBody TSysDaUserRole userRole) {
        String methodCode = "/addBatchRoleUser";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_ADD,userRole.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.addBatchRoleUser(userRole);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRoleIds
     * @return
     */
    @PostMapping("/deleteUserRoleByIds")
    @ResponseBody
    public ResponseData deleteUserRoleByIds(@RequestParam(value = "userRoleIds") String userRoleIds) {
        String methodCode = "/deleteUserRoleByIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"ids："+userRoleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.deleteUserRoleByIds(userRoleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param userIds
     * @return
     */
    @PostMapping("/deleteUserRoleByUserIds")
    @ResponseBody
    public ResponseData deleteUserRoleByUserIds(@RequestParam(value = "userIds") String userIds,
                                            @RequestParam(value = "roleIds") String roleIds) {
        String methodCode = "/deleteUserRoleByUserIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"userIds："+userIds+"; roleIds: "+roleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.deleteUserRoleByUserIds(userIds,roleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param roleIds
     * @return
     */
    @PostMapping("/deleteUserRoleByRoleIds")
    @ResponseBody
    public ResponseData deleteUserRoleByRoleIds(@RequestParam(value = "userIds") String userIds,
                                                @RequestParam(value = "roleIds") String roleIds) {
        String methodCode = "/deleteUserRoleByRoleIds";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,getClass(),
                    SbCode.OPERATION_DELETE,"userIds："+userIds+"; roleIds: "+roleIds,null,null);
            this.sendInitRabbitMqLog(mqDto);
            this.userRoleService.deleteUserRoleByRoleIds(userIds,roleIds);
            commonResponse = CommonResponse.success();
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }
    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRole
     * @return
     */
    @PostMapping("/getAllList")
    @ResponseBody
    public ResponseData getAllList(@RequestBody UserRoleDto userRole) {
        String methodCode = "/getAllList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,userRole.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.userRoleService.getUserRoleList(userRole)
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    @PostMapping("/getUsRoleList")
    @ResponseBody
    public ResponseData getUsRoleList(@RequestBody UserRoleDto userRole) {
        String methodCode = "/getUsRoleList";
        try{
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.userRoleService.getUserRoleList(userRole)
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }
        return new ResponseUtil<>().setData(commonResponse);
    }

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param userRole
     * @return
     */
    @PostMapping("/getUserRoleList")
    @ResponseBody
    public ResponseData getUserRoleList(@RequestBody UserRoleDto userRole) {
        String methodCode = "/getUserRoleList";
        MQDto mqDto = null;
        try{
            mqDto = new MQDto(methodCode,getLoginUserId(),MODULE_CODE,MODULE_NAME,
                    getClass(),SbCode.OPERATION_QUERY,userRole.toString(),null,null);
            this.sendInitRabbitMqLog(mqDto);
            commonResponse = CommonResponse.success();
            commonResponse.setData(
                    this.userRoleService.getPageUserRoleList(
                            UserRolePage.builder()
                                    .userRole(userRole)
                                    .page(this.getPageInfo(userRole))
                                    .build()
                    )
            );
            log.info(methodCode, commonResponse.toString());
        }catch (Exception e){
            e.printStackTrace();
            this.sendAbnormalRabbitMqLog(mqDto.addException(e));
            log.error(methodCode+"/n", e);
            commonResponse = CommonResponse.error();
        }finally {
            this.sendFinishRabbitMqLog(mqDto.addResult(commonResponse));
        }
        return new ResponseUtil<>().setData(commonResponse);
    }


}
